import requests
import json
import datetime


def post_data_to_elasticsearch(index_name, pdict):
    now = datetime.datetime.now(datetime.timezone.utc)
    estyle_time = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + 'Z'
    headers = {'Host': '192.168.1.100:9200',
               'Content-Type': 'application/json'}

    index = {"index": {"_index": index_name, "_type": "doc"}}

    data_dict = {"@timestamp": estyle_time}
    data_dict.update(pdict)

    body_data = json.dumps(index) + '\n' + json.dumps(data_dict) + '\n'

    r = requests.post('http://192.168.1.100:9200/_bulk', headers=headers, data=body_data)

    print(r.json())


if __name__ == "__main__":
    post_dict = {'qytang_location': 'SH', 'qytang_sec_teacher': 'Ender'}
    post_data_to_elasticsearch('qytang_index', post_dict)
